package com.ly.mpayment.core.util;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;


public class StringSort implements Comparable<StringSort> {
	static Logger logger = Logger.getLogger(StringSort.class);

	private String string;

	public void setString(String string) {
		this.string = string;
	}

	public String getString() {
		return string;
	}

	// 构造方法
	public StringSort(String string) {
			this.string = string;
		}

	// 比较
	public int compareTo(StringSort other) {
		// TODO Auto-generated method stub
		char[] thisarr = this.string.toCharArray(); // 存储被包装 字符串的字符数组

		char[] otherarr = other.getString().toCharArray(); // 存储要比较的字符串的字符数组

		/* 取得循环次数，为两个字符串的长度的最小值 */
		int iterate = thisarr.length < otherarr.length ? thisarr.length : otherarr.length;

		boolean mlowercase; // 记录被封装的字符串循环到的字符是否为小写

		boolean olowercase; // 记录要比较的字符串循环到的字符是否为小写

		char thisletter; // 记录被封装的字符串循环到的字符

		char otherletter; // 记录要比较的字符串循环到的字符

		/* 字符串相等，则返回0 */
		if (this.getString().equals(other.getString())) {
			return 0;
		}

		/* 循环字符串，做比较 */
		for (int i = 0; i < iterate; i++) {
			mlowercase = this.isLowercase(thisarr[i]);
			olowercase = this.isLowercase(otherarr[i]);

			/* 把比较字符变成大写 */
			thisletter = mlowercase ? (char) (thisarr[i] - 32) : thisarr[i];
			otherletter = olowercase ? (char) (otherarr[i] - 32) : otherarr[i];

			/* 比较 */
			if (thisletter != otherletter) { // 比较字母大小，不相等，则取差值，字母小的在前面
				return (thisletter - otherletter);
			} else { // 字母的大写形式相同
				if (mlowercase == olowercase) { // 此位置大小写形式相同,判断下一个字符；
					continue;
				} else if (mlowercase) { // 被封装的字符为小写,则返回负值
					return 32;
				} else if (olowercase) { // 比较字符串的字符为小写，则返回正直
					return -32;
				}
			}

		}

		/* 如果循环好之后还分不出大小，则小的排在后面 */
		return (thisarr.length < otherarr.length ? -1 : 1);
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return string;
	}

	// 通过码值，来判断字符是否为小写字母
	private boolean isLowercase(char ch) {
		if ((int) ch >= 97 && (int) ch <= 122) {
			return true;
		} else {
			return false;
		}
	}


	/**
	 * @param json
	 *            参数(已经升序, 排出非空值和sign)
	 * @return MD5的签名字符串(大写)
	 */
	public static String doSign(final JSONObject json) {

		Map<String, String> params = filterSignParams(json);

		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, String> entry : params.entrySet()) {
			if (entry.getValue() != null && !StringUtils.isBlank(entry.getValue()) && !entry.getKey().equals("msg") && !entry.getKey().equals("sign")) {
				sb.append(entry.getKey()).append('=').append(entry.getValue()).append("&");
			}
		}
		String temp = sb.substring(0, sb.length() - 1);
		return temp;
	}

	/**
	 * 签名key=value& 拼接 不处理空值
	 * @param json
	 * @return
	 */
	public static String doSignKey(final JSONObject json) {

		Map<String, String> params = filterParams(json);

		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, String> entry : params.entrySet()) {
			if (entry.getValue() != null  && !StringUtils.isBlank(entry.getValue()) &&  !entry.getKey().equals("msg")) {
				sb.append(entry.getKey()).append('=').append(entry.getValue()).append("&");
			}
		}
		String temp = sb.substring(0, sb.length() - 1);
		return  sb.toString();
	}


	/**
	 * 签名keyvalue拼接 不处理空值
	 * @param json
	 * @return
	 */
	public static String doSignKeyValue(final JSONObject json) {

		Map<String, String> params = filterSignParams(json);

		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, String> entry : params.entrySet()) {
			if (entry.getValue() != null && !StringUtils.isBlank(entry.getValue()) ) {
				sb.append(entry.getKey()).append(entry.getValue());
			}
		}
		return  sb.toString();
	}

	/**
	 * 可以为空值
	 *
	 * @param json
	 * @return
	 */
	public static String doSignKeyValueNull(final JSONObject json) {

		Map<String, String> params = filterParamsNull(json);

		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, String> entry : params.entrySet()) {
			sb.append(entry.getKey()).append(entry.getValue());
		}
		return  sb.toString();
	}

	/**
	 * 签名拼接value值，不处理为空数据
	 * @param json
	 * @return
	 */
	public static String doSignValue(final JSONObject json) {

		Map<String, String> params = filterSignParams(json);

		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, String> entry : params.entrySet()) {
			if (entry.getValue() != null && !StringUtils.isBlank(entry.getValue()) ) {
				sb.append(entry.getValue());
			}
		}
		return  sb.toString();
	}


	/**
	 * 过滤签名参数(升序，排出空值和sign)
	 *
	 * @param json
	 *            待校验参数
	 * @return 过滤后的参数
	 */
	private static Map<String, String> filterSignParams(final JSONObject json) {

		Map<String, String> params = new HashMap<String, String>();
		for(String key : json.keySet()){
			if(json.get(key) != null && !json.get(key).equals("")){
				params.put(key, json.get(key).toString());
			}
		}

		Map<String, String> validParams = new TreeMap<String, String>();
		for (Map.Entry<String, String> param : params.entrySet()) {
			if ("sign".equals(param.getKey()) || StringUtils.isBlank(param.getValue())) {
				continue;
			}
			validParams.put(param.getKey(), param.getValue());
		}
		return validParams;
	}

	/**
	 * json转字符 只处理数据不能为空
	 * @param json
	 * @return
	 */
	public static String jsonToStringNotNull(final JSONObject json) {

		Map<String, String> params = filterParams(json);

		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, String> entry : params.entrySet()) {
			if (entry.getValue() != null && !StringUtils.isBlank(entry.getValue()) ) {
				sb.append(entry.getKey()).append('=').append(entry.getValue()).append("&");
			}
		}
		String temp = sb.substring(0, sb.length() - 1);
		return temp;
	}

	/**
	 * json转字符拼接  格式 x=xx&
	 * @param json
	 * @return
	 */
	public static String jsonToString(final JSONObject json) {

		Map<String, String> params = filterParams(json);

		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, String> entry : params.entrySet()) {
			if (entry.getValue() != null && !StringUtils.isBlank(entry.getValue()) ) {
				sb.append(entry.getKey()).append('=').append(entry.getValue()).append("&");
			}
		}
		String temp = sb.substring(0, sb.length() - 1);
		return temp;
	}
	public static String jsonToStringNotSort(final JSONObject json) {

		StringBuilder sb = new StringBuilder();
		for(String key : json.keySet()){
				sb.append(key).append('=').append(json.getString(key)).append("&");
		}
		String temp = sb.substring(0, sb.length() - 1);
		return temp;
	}

	/**
	 * 中国联通专用
	 * @param json
	 * @return
	 */
	public static String jsonToStringChinaunicom(final JSONObject json) {

		Map<String, String> params = filterParams(json);

		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, String> entry : params.entrySet()) {
			if (entry.getValue() != null && !StringUtils.isBlank(entry.getValue()) ) {
				sb.append(entry.getKey()).append('=').append(entry.getValue()).append("$");
			}
		}
		String temp = sb.substring(0, sb.length() - 1);
		return temp;
	}



	public static String doChinaunicom(final JSONObject json) {

		Map<String, String> params = filterParamsNull(json);

		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, String> entry : params.entrySet()) {
			sb.append(entry.getValue()).append("$");
		}
		return sb.substring(0, sb.length() - 1);
	}

	/**
	 * 可以为空值
	 * @param json
	 * @return
	 */
	private static Map<String, String> filterParamsNull(final JSONObject json) {

		Map<String, String> params = new HashMap<String, String>();
		for(String key : json.keySet()){
			params.put(key,json.get(key) == null ? "" : json.get(key).toString());
		}

		Map<String, String> validParams = new TreeMap<String, String>();
		for (Map.Entry<String, String> param : params.entrySet()) {
			validParams.put(param.getKey(), param.getValue());
		}
		return validParams;
	}

	/**
	 * 不处理空值
	 * @param json
	 * @return
	 */
	private static Map<String, String> filterParams(final JSONObject json) {

		Map<String, String> params = new HashMap<String, String>();
		for(String key : json.keySet()){
			if(json.get(key) != null && !json.get(key).equals("")){
				params.put(key, json.get(key).toString());
			}
		}
		return params;
	}

}
